<template>
    <div>
        <el-table :data="menuList" style="width: 100%">
            <el-table-column type="index" label="编号" width="60px" />
            <el-table-column prop="menuName" label="菜单名称" />
            <el-table-column prop="menuUrl" label="菜单Url" />
            <el-table-column prop="pId" label="父级菜单名称">
                <template v-slot="scope">
                    {{ scope.row.parentName == null ? '无' : scope.row.parentName.menuName }}
                </template>
            </el-table-column>
            <el-table-column prop="createUser" label="创建人" />
            <el-table-column prop="createTime" label="创建日期">
                <template v-slot="scope">
                    {{ scope.row.createTime.substring(0, 10) + " " + scope.row.createTime.substring(11, 19) }}
                </template>
            </el-table-column>
            <el-table-column prop="address" label="修改人" />
            <el-table-column prop="updateTime" label="修改日期">
                <template v-slot="scope">
                    {{ scope.row.updateTime ? scope.row.updateTime.substring(0, 10) + " " +
                        scope.row.updateTime.substring(11,
                            19) : null }}
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template #default="scope">
                    <el-button link size="small" type="primary" @click="upd(scope.row)">修改</el-button>
                    <el-button link size="small" type="danger" @click="delMenu(scope.row.menuId)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 修改的对话框 -->
        <el-dialog
    v-model="dialogVisible"
    title="更新菜单"
    width="500"
  >
    
    <template #footer>
      <div class="dialog-footer">
        <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    
    <el-form-item label="父级菜单" prop="pId">
      <el-select v-model="ruleForm.pId" placeholder="请选择父级菜单">
        <el-option  label="顶级菜单" :value="0" />
        <el-option v-for="item in menuList" :label="item.menuName" :value="item.menuId" />
      </el-select>
    </el-form-item>
    <el-form-item label="菜单名称" prop="menuName">
      <el-input v-model="ruleForm.menuName" />
    </el-form-item>
    <el-form-item label="跳转的Url地址" prop="menuUrl">
      <el-input v-model="ruleForm.menuUrl" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        确定
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">取消</el-button>
    </el-form-item>
  </el-form>
      </div>
    </template>
  </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import {axios} from '@/axios/axiosHelp'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
const router = useRouter()




//删除
const delMenu = (id: any) => {

    ElMessageBox.confirm(
        '确定删除吗?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            axios.get('http://localhost:7081/api/User/QueryPid', {
                params: {
                    MenuId: id
                }
            }).then((res => {
                if (res.data.data>0) {
                    ElMessageBox.confirm(
                        '此菜单有多个子集,确定要删除全部吗?',
                        '警告',
                        {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning',
                        }
                    )
                        .then(() => {
                            axios.post('https://localhost:7181/api/User/DelMenu', {
                                menuId: id
                            }).then((res => {
                                if (res.data.code == 100) {
                                    ElMessage.success('删除成功')
                                    getMenuList()
                                } else {
                                    ElMessage.error('删除失败')
                                }
                            }))
                        })
                }else{
                    axios.post('https://localhost:7181/api/User/DelMenu', {
                        menuId: id
                    }).then((res => {
                        if (res.data.code == 100) {
                                    ElMessage.success('删除成功')
                                    getMenuList()
                                } else {
                                    ElMessage.error('删除失败')
                                }
                    }))
                }
            }))
        })



}

const dialogVisible=ref(false)
//反填
const upd=(row:any)=>{
 dialogVisible.value=true
 ruleForm.menuId=row.menuId
 ruleForm.menuName=row.menuName
 ruleForm.menuUrl=row.menuUrl
 ruleForm.pId=row.pId
 ruleForm.orderNum=row.orderNum
ruleForm.createUser=row.createUser
 ruleForm.createTime=row.createTime
 
}
const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()

const ruleForm = reactive({
   
 "menuId": 0,
  "menuName": "",
  "menuUrl": "",
  "pId": null,
  "orderNum": 0,
  "createUser": "",
  "createTime": "",
  "updateUser": "",



})


//获取菜单列表
const menuList:any = ref([])
const getMenuList = () => {
    axios.get('http://localhost:7081/api/User/QueryMenu').then((res => {
        menuList.value = res.data.data
    }))
}

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
        axios.post('/wtest/api/User/UpdMenu',ruleForm).then((res=>{
        if(res.data.data==1){
            ElMessage.success(res.data.msg)
            dialogVisible.value=false
            getMenuList()
        }
        if(res.data.data==0){
            ElMessage.error(res.data.msg)
        }
    }))
    } else {
      console.log('error submit!', fields)
    }
  })
}               

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}



const rules = reactive<FormRules>({
    menuName: [
    { required: true, message: '请输入菜单名称', trigger: 'blur' },
    
  ],
  pId: [
    {
      required: true,
      message: '请选择父级菜单',
      trigger: 'change',
    },
  ],
  menuUrl: [
    { required: true, message: '请输入跳转的Url地址', trigger: 'blur' },
    
  ],
})

onMounted(()=>{
    getMenuList()
})

</script>

<style scoped></style>